package com.techysky.controller;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.techysky.entity.DataModule;
import com.techysky.entity.PublicModule;
import com.techysky.service.DataModuleService;
import com.techysky.service.PublicModuleService;
import com.techysky.service.TreeService;


@Controller
@RequestMapping("/tree")
public class TreeController implements Serializable {

	private static final String ITEMS = "items";
	private static final String DMROWS = "dmrows";
	/**
	 * @author nichao
	 * @category
	 */
	private static final long serialVersionUID = 1L;
	@Autowired
	private TreeService treeService;
	@Autowired
	private PublicModuleService publicModuleService;	
	@Autowired
	private DataModuleService dataModuleService;	
	/**
	 * 初始化树
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/initTree/{projectId}")
	@ResponseBody
	public Object initTree(@PathVariable(value="projectId")  String projectId,HttpServletRequest request){
		return this.treeService.initTree(projectId,request);
	}
	/**
	 * 适用性过滤初始化树
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/initTree")
	@ResponseBody
	public Object initTree(@RequestParam(value="projectId")  String projectId,@RequestParam(value="fitName")  String fitName,@RequestParam(value="type")  String type,HttpServletRequest request){
		return this.treeService.initTree(projectId,fitName,type,request);
	}
	
	/**
	 * 获取树下级
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/getChildrenTrees")
	@ResponseBody
	public Object getChildrenTrees(HttpServletRequest request){
		return this.treeService.getChildrenTrees(request);
	}
	@RequestMapping("/initList")
	public String getRootElement(@RequestParam(value = "id") String id, Model model){
		
		List<PublicModule> pms = publicModuleService.findRootElement(id);
		model.addAttribute("rows", pms);
			
		return "quicknavigation";
	}
	@RequestMapping("/getChildrenData")
	public String getChildrenData(@RequestParam(value = "id") String id,@RequestParam(value = "nodeName") String nodeName,
			@RequestParam(value = "level") int level, Model model){
		
		int lev = level+1;
		List<PublicModule> pms =null ;//publicModuleService.findByParentId(id);
		List<DataModule> dms = dataModuleService.findByPublicModuleId(id);
		model.addAttribute("title", nodeName);
		model.addAttribute("prows", pms);	
		model.addAttribute("drows", dms);	
		model.addAttribute("level", lev);			
		return "quicknavigation2";
	}
	@RequestMapping("/getChildrenList")
	public String getChildrenList(@RequestParam(value = "id") String id, Model model,HttpServletRequest request){
		HttpSession session = request.getSession();
		String guolvFit=null!=session.getAttribute("guolvFit")?session.getAttribute("guolvFit").toString():"";
		if(!"".equals(guolvFit)){
			List<DataModule> dms = dataModuleService.findByPublicModuleId(id);
			for (int i=0;i<dms.size();i++) {
				if(!"all".equals(dms.get(i).getFitName())&&!guolvFit.contains(dms.get(i).getFitName())){
					dms.remove(i);
				}
			}
			model.addAttribute(DMROWS, dms);
		}
		else{
		List<DataModule> dms = dataModuleService.findByPublicModuleId(id);
		model.addAttribute(DMROWS, dms);
		}
		List<PublicModule> pms =null;// publicModuleService.findByParentId(id);
		model.addAttribute("pmrows", pms);
	
		return "recautions";
	}
	@RequestMapping("/getChildrenDm")
	public String getChildrenDm(@RequestParam(value = "id") String id, Model model){
		
		List<DataModule> dms = dataModuleService.findByPublicModuleId(id);
		model.addAttribute(DMROWS, dms);
		return "";
	}
	/**
	 * 获取一级PM列表
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getPmFirstLevel")
	public Object getPmFirstLevel(@RequestParam(value = "id") String id){
		Map<String,Object> result=new HashMap<>();
		if("homeFirstLevel".equals(id)){
			result.put(ITEMS,publicModuleService.findByParentIdJql(null));
		}
		else{
		result.put(ITEMS,publicModuleService.findByParentIdJql(id));
		}
		return result;
	}
	/**
	 * 获取pm下所有dm，用于试卷关联dm
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getAllDm")
	public Object getAllDm(@RequestParam(value = "id") String id,HttpServletRequest request){
		int size=0;
		int page=0;
		if(null!=request.getParameter("page")&&null!=request.getParameter("size")){
		page=Integer.parseInt(request.getParameter("page")); 
		size=Integer.parseInt(request.getParameter("size"));
		}
		List<PublicModule> pms = this.treeService.getAllDm(id);
		return this.dataModuleService.queryAllDm(pms, request.getParameter("title"), page, size);
	}
	}
